package org.ghost.wechat.platform.controller;

import org.ghost.wechat.platform.dto.RspDTO;
import org.ghost.wechat.platform.util.JacksonUtil;
import org.ghost.wechat.platform.util.MapResource;
import org.springframework.web.bind.annotation.*;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.Map;

@RestController
@RequestMapping(value = "demo")
public class HelloDemoController extends BaseController {
    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public RspDTO helloWorld(@RequestParam(required = false) String msg) {
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return this.render("Hello World" + msg);
    }

    @RequestMapping(value = "hello", method = RequestMethod.POST)
    public RspDTO helloWorld(@RequestBody Map<String, Object> reqMap) {
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return this.render(JacksonUtil.useDefaultMapper().toJson(reqMap));
    }

    @RequestMapping(value = "mem", method = RequestMethod.GET)
    public RspDTO helloWorld() {
        MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
        return this.render(new MapResource<String, Long>()
                .add("init", memoryUsage.getInit())
                .add("max", memoryUsage.getMax())
                .add("used", memoryUsage.getUsed())
                .add("committed", memoryUsage.getCommitted()).toMap());
    }

}

